package org.zn.note.jdk.others;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @ClassName GeShui
 * @Author zn
 * @Date 2019/11/13 10:14
 **/
public class GeShui {
    // 下面四个手动设置
//    private static final long GZ = 29680; // 税前工资
//    private static final long GJJ = 1449; // 公积金个人缴纳
//    private static final long SB = 418; // 五险个人缴纳
//    private static final long ZXKJ = 5000; // 个税专项扣减
    private static final long GZ = 27560; // 税前工资
    private static final long GJJ = 1545; // 公积金个人缴纳
    private static final long SB = 411; // 五险个人缴纳
    private static final long ZXKJ = 2000; // 个税专项扣减

    // 扣掉五险一金的工资（静态数据）
    private static final long GZ2 = GZ - GJJ - SB;
    // 纳税起征点（静态数据）
    private static final long NSQZD = 5000;
    // 每期纳税基数（静态数据）
    private static final long NSJS = GZ2 - ZXKJ - NSQZD;
    // 阶梯费率（静态数据）
    private static final Map<Long, double[]> FLB = new LinkedHashMap();

    // 初始化阶梯费率（静态数据）
    static {
        FLB.put(36000L, new double[]{0, 0.03});
        FLB.put(144000L, new double[]{2520, 0.10});
        FLB.put(300000L, new double[]{16920, 0.20});
        FLB.put(420000L, new double[]{31920, 0.25});
        FLB.put(660000L, new double[]{52920, 0.30});
        FLB.put(960000L, new double[]{85920, 0.35});
        FLB.put(Long.MAX_VALUE, new double[]{181920, 0.45});
    }

    // 根据当前累计的纳税额，找到对应的阶梯费率
    private static long getFLBKey(long amount) throws Exception {
        for (long key : FLB.keySet()) {
            if (key >= amount)
                return key;
        }
        throw new Exception("Not Find Key !");
    }

    public static void main(String[] args) throws Exception {

        long sum = 0L; // 累计的阶梯费率
        long nseSum = 0L; // 累计纳税额
        long shgzSum = 0L; // 累计税后工资
        for (int i = 1; i < 13; i++) { // 12个月
            sum += NSJS;
            long key = getFLBKey(sum);
            double sjs = FLB.get(key)[0]; // 速减数
            double fl = FLB.get(key)[1]; // 费率
            long nse = (long) (sum * fl - sjs) - nseSum; // 纳税额
            nseSum += nse;
            long shgz = GZ2 - nse; // 税后工资
            shgzSum += shgz;
            System.out.printf("%02d月：累计应税[%06d] 税后工资[%d], 费率区间[%06d], 个税[%04d]\n",
                    i, sum, shgz, key, nse);
        }
        System.out.printf("税前工资：%d, 税后工资：%d, 公积金：%d, 最后一年到手：%d", GZ * 12, shgzSum, GJJ * 24, shgzSum + GJJ * 24);
    }
}
